/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package hirt;

import java.awt.Point;
import java.nio.ByteBuffer;
import java.util.Vector;
/**
 * MovementActions are used when an item moves from one point on the map to
 * another point on the map
 * @author zamille
 */
public class MovementAction extends Action
{
    /** The point the actor started at*/
    protected Point startPoint;
    /** The point the actor ended at*/
    protected Point endPoint;
    /** The angle the actor in*/
    protected float angle;

    /**
     *
     */
    public MovementAction()
    {
        super();
        startPoint = null;
        endPoint = null;
        angle = 0;
        super.messageType = Message.MOVE_MESSAGE;
    }

    /**
     *
     * @param actor
     * @param x
     * @param y
     */
    public MovementAction(Item actor, int x, int y)
    {
        super(actor);
        endPoint = new Point(x,y);
        startPoint = null;
        angle = 0;
        super.messageType = Message.MOVE_MESSAGE;
    }

    /**
     *
     * @param actor
     * @param endPoint
     */
    public MovementAction(Item actor, Point endPoint)
    {
        super(actor);
        this.endPoint = endPoint;
        startPoint = null;
        angle = 0;
        super.messageType = Message.MOVE_MESSAGE;
    }

    /**
     *
     * @param actor
     * @param startTime
     * @param duration
     * @param x
     * @param y
     */
    public MovementAction(Item actor, double startTime, double duration, int x, int y)
    {
        super(actor,startTime,duration);
        endPoint = new Point(x,y);
        startPoint = null;
        angle = 0;
        super.messageType = Message.MOVE_MESSAGE;
    }

    /**
     *
     * @param actor
     * @param startTime
     * @param duration
     * @param x
     * @param y
     * @param angle
     */
    public MovementAction(Item actor, double startTime, double duration, int x, int y, float angle)
    {
        super(actor,startTime,duration);
        endPoint = new Point(x,y);
        startPoint = null;
        this.angle = angle;
        super.messageType = Message.MOVE_MESSAGE;
    }

    /**
     *
     * @param actor
     * @param startTime
     * @param duration
     * @param endPoint
     */
    public MovementAction(Item actor, double startTime, double duration, Point endPoint)
    {
        super(actor,startTime,duration);
        this.endPoint = endPoint;
        startPoint = null;
        angle = 0;
        super.messageType = Message.MOVE_MESSAGE;
    }

    /**
     *
     * @param actor
     * @param startTime
     * @param duration
     * @param endPoint
     * @param angle
     */
    public MovementAction(Item actor, double startTime, double duration, Point endPoint, float angle)
    {
        super(actor,startTime,duration);
        this.endPoint = endPoint;
        startPoint = null;
        this.angle = angle;
        super.messageType = Message.MOVE_MESSAGE;
    }

    /**
     *
     * @param actor
     * @param startTime
     * @param duration
     * @param endPoint
     * @param startPoint
     */
    public MovementAction(Item actor, double startTime, double duration, Point endPoint, Point startPoint)
    {
        super(actor,startTime,duration);
        this.endPoint = endPoint;
        this.startPoint = startPoint;
        angle = 0;
        super.messageType = Message.MOVE_MESSAGE;
    }

    /**
     *
     * @param actor
     * @param startTime
     * @param duration
     * @param endPoint
     * @param startPoint
     * @param angle
     */
    public MovementAction(Item actor, double startTime, double duration, Point endPoint, Point startPoint, float angle)
    {
        super(actor,startTime,duration);
        this.endPoint = endPoint;
        this.startPoint = startPoint;
        this.angle = angle;
        super.messageType = Message.MOVE_MESSAGE;
    }

    /**
     * Builds an Action from a byte stream, presumably created by an action across the network
     * this action should be identical to that one
     * @param bytes
     * @param items: Master Item List used to build the action
     */
    public MovementAction(ByteBuffer buf, Vector<Item> items)
    {
        super(buf, items);              //it uses the inherited method to build
        startPoint = ByteChanger.getPoint(buf, 20);  //the inherited members from the ByteBuffer
        endPoint = ByteChanger.getPoint(buf, 28);
        angle = buf.getFloat(36);
        super.messageType = Message.MOVE_MESSAGE;
    }

    /**
     * Builds an Action from a save string, presumably read from a save
     * this action should be identical to the one saved in the file
     * @param save string
     * @param items: Master Item List used to build the action
     * @return what's left of the parem SaveString after this methods completes
     */
    MovementAction(String substring, Vector<Item> itemList) {
        fromSaveString(substring, itemList);
    }

    public float getAngle() {
        return angle;
    }

    public void setAngle(float angle) {
        this.angle = angle;
    }

    public Point getEndPoint() {
        return endPoint;
    }

    public void setEndPoint(Point endPoint) {
        this.endPoint = endPoint;
    }

    public Point getStartPoint() {
        return startPoint;
    }

    public void setStartPoint(Point startPoint) {
        this.startPoint = startPoint;
    }

    /**
     * Returns a ByteBuffer representation of this Action
     * Used to send actions over the network
     * @return ByteBuffer
     */
    @Override
    public ByteBuffer toByte()
    {
        ByteBuffer buf = super.toByte();   //it uses the inherited method to put
        buf.put(ByteChanger.pointToByte(startPoint));  //the inherited members in the ByteBuffer
        buf.put(ByteChanger.pointToByte(endPoint));
        buf.putFloat(angle);
        return buf;
    }

    /**
     * Returns a String representation of this Action
     * Used to save this action to a file
     * @return Save String
     */
    @Override
    public String toSaveString()
    {
        String text = super.toSaveString();   //it uses the inherited method to put
        text += startPoint.x  + "\t";        //the inherited members in the Save String
        text += startPoint.y  + "\t";
        text += endPoint.x  + "\t";
        text += endPoint.y  + "\t";
        text += angle + "t";
        return text;
    }

    /**
     * Builds an Action from a save string, presumably read from a save
     * this action should be identical to the one saved in the file
     * @param save string
     * @param items: Master Item List used to build the action
     * @return what's left of the parem SaveString after this methods completes
     */
    @Override
    protected String fromSaveString(String text, Vector<Item> items)
    {
        text = super.fromSaveString(text, items);   //it uses the inherited method to build
        String substring = ByteChanger.getNextValue(text);  //the inherited members from the Save String
        int x = Integer.parseInt(substring);

        text = ByteChanger.incrementString(text);
        substring = ByteChanger.getNextValue(text);
        int y = Integer.parseInt(substring);
        startPoint = new Point(x,y);

        text = ByteChanger.incrementString(text);
        substring = ByteChanger.getNextValue(text);
        x = Integer.parseInt(substring);

        text = ByteChanger.incrementString(text);
        substring = ByteChanger.getNextValue(text);
        y = Integer.parseInt(substring);
        endPoint = new Point(x,y);

        text = ByteChanger.incrementString(text);
        substring = ByteChanger.getNextValue(text);
        angle = Float.parseFloat(substring);

        text = ByteChanger.incrementString(text);
        return text;
    }

}
